// pages/finance/profile/account/account.js
const financeAccount = require('../../../../utils/finance-account');
Page({
  data: {
    accountList: [],
    limit: 4,
    newAccountName: '',
    showEditModal: false,
    editAccountName: '',
    editIndex: -1,
  },
  async onLoad() {
    await this.initData();
  },
  async initData() {
    const res = await financeAccount.accountInit();
    this.setData({
      accountList: res.list || [],
      limit: res.limit || 4,
      newAccountName: '',
      showEditModal: false,
      editAccountName: '',
      editIndex: -1,
    });
  },
  onNewAccountInput(e) {
    this.setData({ newAccountName: e.detail.value });
  },
  async onAddAccount() {
    const name = this.data.newAccountName.trim();
    if (!name) return wx.showToast({ title: '请输入账号名称', icon: 'none' });
    if (name.length > 10) return wx.showToast({ title: '最多10个字', icon: 'none' });
    if (this.data.accountList.length >= this.data.limit) return wx.showToast({ title: '已达上限', icon: 'none' });
    wx.showLoading({ title: '新建中...' });
    try {
      await financeAccount.accountAdd({ accountName: name });
      wx.hideLoading();
      // 清空本地的账户缓存
      wx.removeStorageSync('accountBooks');
      await this.initData();
    } catch (e) {
      console.log(e)
      wx.hideLoading();
      wx.showToast({ title: (e && e.data && e.data.msg) || '新建失败', icon: 'none' });
    }
  },
  onEditAccount(e) {
    const idx = e.currentTarget.dataset.index;
    const item = this.data.accountList[idx];
    this.setData({
      showEditModal: true,
      editAccountName: item.accountName,
      editIndex: idx
    });
  },
  onEditAccountInput(e) {
    this.setData({ editAccountName: e.detail.value });
  },
  // 编辑
  async onEditAccountConfirm() {
    const idx = this.data.editIndex;
    const name = this.data.editAccountName.trim();
    if (!name) return wx.showToast({ title: '请输入新名称', icon: 'none' });
    if (name.length > 10) return wx.showToast({ title: '最多10个字', icon: 'none' });
    wx.showLoading({ title: '编辑中...' });
    try {
      await financeAccount.accountEdit({ id: this.data.accountList[idx].id, accountName: name });
      wx.hideLoading();
      // 清空本地的账户缓存
      wx.removeStorageSync('accountBooks');
      await this.initData();
    } catch (e) {
      console.log(e)
      wx.hideLoading();
      wx.showToast({ title: (e && e.data && e.data.msg) || '编辑失败', icon: 'none' });
    }
  },
  onEditAccountCancel() {
    this.setData({ showEditModal: false, editAccountName: '', editIndex: -1 });
  },
  async onDelAccount(e) {
    const idx = e.currentTarget.dataset.index;
    const item = this.data.accountList[idx];
    wx.showModal({
      title: '删除账号',
      content: '确定要删除该账户吗？这是不可逆操作，删除后该账户下已经记录的数据将无法查看。',
      success: async (res) => {
        if (res.confirm) {
          wx.showLoading({ title: '删除中...' });
          try {
            await financeAccount.accountDelete(item.id);
            wx.hideLoading();
            // 清空本地的账户缓存
            wx.removeStorageSync('accountBooks');
            await this.initData();
          } catch (e) {
            console.log(e)
            wx.hideLoading();
            wx.showToast({ title: (e && e.data && e.data.msg) || '删除失败', icon: 'none' });
          }
        }
      }
    });
  },
  // 设置默认
  async onSetDefaultAccount(e) {
    const idx = e.currentTarget.dataset.index;
    const item = this.data.accountList[idx];
    wx.showLoading({ title: '设置中...' });
    try {
      await financeAccount.setDefaultFlag(item.id);
      wx.hideLoading();
      // 清空本地的账户缓存
      wx.removeStorageSync('accountBooks');
      await this.initData();
    } catch (e) {
      console.log(e)
      wx.hideLoading();
      wx.showToast({ title: (e && e.data && e.data.msg) || '设置失败', icon: 'none' });
    }
  },
});